package com.wanmait.schedule.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.wanmait.schedule.entity.CompanyUser;
import com.wanmait.schedule.entity.Department;
import com.wanmait.schedule.service.DepartmentService;
import com.wanmait.schedule.util.Result;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.ResultSet;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author wanmait
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Resource
    private DepartmentService departmentService;

    @PostMapping("leader/add")
    public Result add(@RequestBody Department department){
        departmentService.save(department);
        return Result.success();
    }
    @GetMapping("leader/list")
    public Result list(Integer pageNum, Integer pageSize, HttpServletRequest request){
        System.out.println("pageSize"+pageSize);
        CompanyUser companyUser = (CompanyUser) request.getAttribute("companyUser");
        Integer companyId = companyUser.getCompanyId();
        PageInfo<Department> pageInfo = departmentService.findPage(pageNum,pageSize,companyId);
        return Result.success(pageInfo);
    }
    @GetMapping("leader/del/{id}")
    public Result del(@PathVariable Integer id){
        departmentService.removeById(id);
        return Result.success();
    }
    @GetMapping("leader/initdata/{id}")
    public Result initdata(@PathVariable Integer id){
        Department department = departmentService.getById(id);
        return Result.success(department);
    }
    @PostMapping("leader/edit")
    public Result edit(@RequestBody Department department,Integer id){
        LambdaUpdateWrapper<Department> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Department::getId,id);
        departmentService.update(department,updateWrapper);
        return Result.success();
    }
}
